package com.github.ecbp.common.validator;

import org.springframework.util.ObjectUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @Author Created by 何德宝 on 2020/4/2 22:25
 * @Version V1.0
 * @Description: 状态标记校验器
 */
public class FlagValidatorClass implements ConstraintValidator<FlagValidator, Integer> {

    private String[] values;

    @Override
    public void initialize(FlagValidator flagValidator) {
        this.values = flagValidator.value();
    }

    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext constraintValidatorContext) {
        boolean isValid = false;
        if(ObjectUtils.isEmpty(value)){
            // 当状态为空时使用默认值
            return true;
        }
        for (String v : values) {
            if(v.equals(String.valueOf(value))){
                isValid = true;
                break;
            }
        }
        return isValid;
    }
}
